﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Animations;
using UnityEngine;
using Random = UnityEngine.Random;

[RequireComponent(typeof(EnemyMotor))]
[RequireComponent(typeof(Enemy))]
public class EnemyAI : MonoBehaviour
{
    private GameObject player;
    private EnemyMotor motor;
    public GameObject left, right;
    public float attackDis;
    private Vector2 lef, rig;
    // Start is called before the first frame update
    public enum State
    {
        patrol,attack
    }

    public State state;

    private void Update()
    {
        switch (state)
        {
            case State.patrol :
                Patrol();
                break;
            case State.attack :
                Attack();
                break;
        }
    }

    private void Start()
    {
        attackDis = 5f;
        left = transform.GetChild(0).gameObject;
        right = transform.GetChild(1).gameObject;
        lef = left.transform.position;
        rig = right.transform.position;
        state = State.patrol;
        motor = GetComponent<EnemyMotor>();
    }

    private void Patrol()//未进入范围内，敌人巡逻
    {
        motor.Move();
        if (transform.position.x - lef.x < 0.1f || transform.position.x - rig.x > 0.1f)
        {
            motor.ChangeDir();//改变方向同时停止
            motor.Stop();
            Invoke("ChangeState",Random.Range(1,2));//改变状态，同时前进
        }
    }

    private void Attack()//进入范围，敌人攻击
    {
        Vector2 player = this.player.transform.position;
        Vector2 enemy = transform.position;
        if (attackDis>Math.Abs(player.x-enemy.x))
        {
            print("stop");
            motor.Stop();
        }
        else
        {
            if (player.x < enemy.x && motor.direct == Vector2.right)
            {
                motor.Stop();
                motor.ChangeDir();
                Invoke("ChangeState",Random.Range(0.8f,1.5f));
            }
            else if (player.x > enemy.x && motor.direct == Vector2.left)
            {
                motor.Stop();
                motor.ChangeDir();
                Invoke("ChangeState", Random.Range(0.8f, 1.5f));
            }
            motor.Move();
        }
        

        
        
    }
    public void ChangeState()
    {
        switch (state)
        {
            case State.patrol:
                motor.temSpeed = motor.patrolSpeed;
                motor.animator.SetBool("jump",true);
                break;
            case State.attack:
                motor.temSpeed = motor.agressiveSpeed;
                motor.animator.SetBool("jump",true);
                break;
        }
        
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.GetComponent<Player_Behavour>())
        {
            player = other.gameObject;
            state = State.attack;
            ChangeState();
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.GetComponent<Player_Behavour>())
        {
            state = State.patrol;
            ChangeState();
        }
    }
}
